<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Wx extends BaseController {
    public function __construct() {
        parent::__construct();

        // 移动端初始化方法
        $this->wx_init();

        $this->load->library('wechat');
    }

    // 下载微信资源图片
    public function download_media() {
        $serverid = $this->get('serverid');
        // 默认为jpg
        $type = $this->get('type') ? $this->get('type') : 'jpg';
        $fileInfo = $this->wechat->download_media($serverid);
        // $param =  array(
        //     'username' => UP_USERNAME,
        //     'password' => UP_PASSWORD,
        //     'bucketname' => UP_BUCKET
        // );
        // // 引入又拍云库
        // $this->load->library('UpYunClass', $param);
        // $filename = date("Y") . "/" . date("m") . "/" . date("d"). "/". mt_rand(10000, 99999) . time() . '.' . $type;
        // // 上传又拍云
        // $this->upyunclass->writeFile("/". $filename, $fileInfo['body'], True);   // 上传图片，自动创建目录

        // var_export($fileInfo);exit;
        $this->load->library('localUploader');
        $webUrl = $this->localuploader->uploadWechatImage($fileInfo['body']);
        if($webUrl == false) {
            throw new Exception('系统错误，请联系管理员');
        }
        exit(json("", true, $webUrl));

        // exit(json("", true, UP_URL. $filename));
    }

    // 下载微信资源音频
    public function download_audio() {
        $serverid = $this->get('serverid');
        // 格式为jpg
        $type = 'mp3';
        $fileInfo = $this->wechat->download_media($serverid);
        // $param =  array(
        //     'username' => UP_USERNAME,
        //     'password' => UP_PASSWORD,
        //     'bucketname' => UP_BUCKET
        // );
        // // 引入又拍云库
        // $this->load->library('UpYunClass', $param);
        // $filename = date("Y") . "/" . date("m") . "/" . date("d"). "/". mt_rand(10000, 99999) . time() . '.' . $type;
        // // 将音频转为MP3
        // $params['x-audio-avopts'] = '/ab/44/ac/2/f/mp3';
        // // 上传又拍云
        // $this->upyunclass->writeFile("/". $filename, $fileInfo['body'], true, $params);   // 上传音频，自动创建目录

        // echo '<pre>';var_export($fileInfo);exit;
        $this->load->library('localUploader');
        $webUrl = $this->localuploader->uploadWechatAudio($fileInfo['body']);
        if($webUrl == false) {
            throw new Exception('系统错误，请联系管理员');
        }
        exit(json("", true, $webUrl));
    }

    // 下载微信资源音频
    public function download_audio_with_duration() {
        $serverid = $this->get('serverid');
        // 格式为jpg
        $type = 'mp3';
        $fileInfo = $this->wechat->download_media($serverid);
        $param =  array(
            'username' => UP_USERNAME,
            'password' => UP_PASSWORD,
            'bucketname' => UP_BUCKET
        );
        // 引入又拍云库
        $this->load->library('UpYunClass', $param);
        $filename = date("Y") . "/" . date("m") . "/" . date("d"). "/". mt_rand(10000, 99999) . time() . '.' . $type;
        // 将音频转为MP3
        $params['x-audio-avopts'] = '/ab/44/ac/2/f/mp3';
        // 上传又拍云
        $this->upyunclass->writeFile("/". $filename, $fileInfo['body'], true, $params);   // 上传音频，自动创建目录

        // 获取时长
        require_once('vendor/autoload.php');
        // 初始化又拍云接口
        $config = new Upyun\Config(UP_BUCKET, UP_USERNAME, UP_PASSWORD);
        $upyun = new Upyun\Upyun($config);
        $upyunInfo = $upyun->avMeta($filename);
        $duration = '00:00';
        if(isset($upyunInfo['format']) && isset($upyunInfo['format']['duration'])) {
            $duration = $upyunInfo['format']['duration'];
            // 向下取整
            $duration = floor($duration);
            // 秒数转换为分秒格式02:01
            $duration = handle_audio_duration($duration);
        }
        $ret = array(
            'link' => UP_URL. $filename,
            'duration' => $duration
        );
        exit(json("", true, $ret)); 
    }
}